<?php

use yii\helpers\Url;

$web = Url::base();

$site_name = \app\models\Mall::getCurrentInstitutionSiteName();
$this->title = $site_name . '-地址管理';
?>


<link rel="stylesheet" type="text/css" href="<?= $web ?>/css/mall-m/user-address.css"/>
<div class="main">
    <input type="hidden" id="txtAddressId" value="<?= $item == null ? '' : $item['id'] ?>">
    <div class="content">
        <div class="content-name">
            <p class="f-l">收货姓名：</p><input id="txtName" class="receiptName f-l" type="text" value="<?= $item == null ? '' : $item['name'] ?>"/>
        </div>
        <div class="content-phone">
            <p class="f-l">联系电话：</p><input id="txtPhone" class="f-l" type="tel" value="<?= $item == null ? '' : $item['phone'] ?>"/>
        </div>
        <div class="content-province">
            <p class="f-l">选择地区：</p>
            <div class="content-province-p f-l">
                <select id="s_province" name="s_province">
                    <option value="">请选择</option>
                </select><br/>
                <select id="s_city" name="s_city">
                    <option value="">请选择</option>
                </select><br/>
                <select id="s_county" name="s_county">
                    <option value="">请选择</option>
                </select><br/>
            </div>
        </div>
        <div class="content-row">
            <p class="f-l">详细地址：</p>
            <textarea autoHeight="true" id="txtAddress"><?= $item == null ? '' : $item['address'] ?></textarea>
        </div>

        <div class="content-remark">
            <p class="f-l">备注：</p>
            <textarea autoHeight="true" id="txtRemark"><?= $item == null ? '' : $item['remark'] ?></textarea>
        </div>
        <script type="text/javascript">
            $.fn.autoHeight = function () {
                function autoHeight(elem) {
                    elem.style.height = 'auto';
                    elem.scrollTop = 0; //防抖动
                    elem.style.height = elem.scrollHeight + 'px';
                }

                this.each(function () {
                    autoHeight(this);
                    $(this).on('keyup', function () {
                        autoHeight(this);
                    });
                });
            };

            $('textarea[autoHeight]').autoHeight();
        </script>
        <a href="javascript:void(0);" id="btnSave">
            <div class="save-btn">
                <input type="button" value="保存"/>
            </div>
        </a>
    </div>
</div>

<script type="application/javascript">
    $(function () {
        $(".receiptName").focus();

        getProvinceSelect();

        $('#s_province').change(function () {
            getCitySelect();
        });

        $('#s_city').change(function () {
            getRegionSelect();
        });

        $('#btnSave').click(function () {
            var name = $('#txtName').val().trim();
            if (name.length == 0) {
                alert('请输入姓名');
                $('#txtName').focus();
                return;
            }

            var phone = $('#txtPhone').val().trim();
            if (phone.length == 0) {
                alert('请输入手机号码');
                $('#txtPhone').focus();
                return;
            }

            var provinceId = $('#s_province').val().trim();
            if (provinceId.length == 0) {
                alert('请选择省');
                return;
            }
            var provinceName = $('#s_province').find('option:selected').text().trim();

            var cityId = $('#s_city').val().trim();
            if (cityId.length == 0) {
                alert('请选择市');
                return;
            }
            var cityName = $('#s_city').find('option:selected').text().trim();

            var regionId = $('#s_county').val().trim();
            if (regionId.length == 0) {
                alert('请选择区');
                return;
            }
            var regionName = $('#s_county').find('option:selected').text().trim();

            var address = $('#txtAddress').val().trim();
            if (address.length == 0) {
                alert('请输入详细地址');
                $('#txtAddress').focus();
                return;
            }

            var remark = $('#txtRemark').val().trim();

            var data = {};
            data.address_id = $('#txtAddressId').val();
            data.name = name;
            data.phone = phone;
            data.province_name = provinceName;
            data.city_name = cityName;
            data.region_name = regionName;
            data.address = address;
            data.remark = remark;
            requestJson('set-user-address-handler', data, true, function (responseData) {
                if (responseData.status == 0) {
//                    history.back();
//                    location.reload();
                    self.location=document.referrer;
                } else {
                    alert('保存失败!');
                }
            });
        });
    });

    function getProvinceSelect() {
        requestJson('get-area-handler', null, true, function (responseData) {
            if (responseData.data != null) {
                var provinceName = "<?= $item == null ? '' : $item['province_name'] ?>";
                $('#s_province').html('');
                $('#s_province').append($('<option value="">请选择</option>'));
                $(responseData.data).each(function () {
                    var item = null;
                    if (this.name == provinceName) {
                        item = $('<option value="' + this.id + '" selected>' + this.name + '</option>');
                    } else {
                        item = $('<option value="' + this.id + '">' + this.name + '</option>');
                    }
                    $('#s_province').append(item);
                });

                getCitySelect();
            }
        });
    }

    function getCitySelect() {
        var areaId = $('#s_province').val();
        if (areaId.length == 0) {
            return;
        }
        requestJson('get-area-handler', {area_id: areaId}, true, function (responseData) {
            if (responseData.data != null) {
                var cityName = "<?= $item == null ? '' : $item['city_name'] ?>";
                $('#s_city').html('');
                $('#s_city').append($('<option value="">请选择</option>'));
                $(responseData.data).each(function () {
                    var item = null;
                    if (this.name == cityName) {
                        item = $('<option value="' + this.id + '" selected>' + this.name + '</option>');
                    } else {
                        item = $('<option value="' + this.id + '">' + this.name + '</option>');
                    }
                    $('#s_city').append(item);
                });
                getRegionSelect();
            }
        });
    }

    function getRegionSelect() {
        var areaId = $('#s_city').val();
        if (areaId.length == 0) {
            $('#s_county').html('');
            $('#s_county').append($('<option value="">请选择</option>'));
            return;
        }
        requestJson('get-area-handler', {area_id: areaId}, true, function (responseData) {
            if (responseData.data != null) {
                var regionName = "<?= $item == null ? '' : $item['region_name'] ?>";
                $('#s_county').html('');
                $('#s_county').append($('<option value="">请选择</option>'));
                $(responseData.data).each(function () {
                    var item = null;
                    if (this.name == regionName) {
                        item = $('<option value="' + this.id + '" selected>' + this.name + '</option>');
                    } else {
                        item = $('<option value="' + this.id + '">' + this.name + '</option>');
                    }
                    $('#s_county').append(item);
                });
            }
        });
    }
</script>